package com.bs.sc.mgr.user.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bs.sc.base.utils.AjaxJson;
import com.bs.sc.mgr.user.entity.MgrUser;
import com.bs.sc.mgr.user.service.MgrUserService;

@RestController
@RequestMapping(value = "/mgrUser")
@CrossOrigin
public class MgrUserController {

	@Autowired
	private MgrUserService mgrUserService;

	/**
	 * 后台管理登陆
	 * 
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "mgrLogin", method = RequestMethod.POST)
	public AjaxJson mgrLogin(MgrUser user, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("用户名或密码错误");
		List<MgrUser> users = mgrUserService.findByUserNameAndPassword(user);
		if (users.size() > 0) {
			j.setCode("success");
			j.setMsg("登陆成功");
			request.getSession().setAttribute("mgrUser", users.get(0));
		}
		return j;
	}

	/**
	 * 获取后台登陆用户session
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "getMgrSession", method = RequestMethod.GET)
	public AjaxJson getMgrSession(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		MgrUser u = (MgrUser) request.getSession().getAttribute("mgrUser");
		System.out.println("mgr session:" + u);
		if (u != null) {
			j.setCode("success");
			j.setMsg("获取session成功");
			j.setData(u);
		} else {
			j.setCode("failure");
			j.setMsg("session超时");
		}
		return j;
	}
}
